﻿using System.Collections.Generic;
using System.Linq;
using GraphProcessor;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Logic;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public abstract class LevelServiceExecutionNode : LevelServiceBaseNode
    {
        public override Color color => new Color(0.9f, 0.5f, 0.1f, 1);

        public ServiceExecutionType serviceType => this.mServiceType;
        protected abstract ServiceExecutionType mServiceType { get; }
        [Input("last", false)] public LevelServiceEditor last;
        [Output("next", false)] public LevelServiceEditor next;

        [LevelServiceEditorCustomProperty("执行组")]
        public string executionGroupID;

        // [Output("目标选择", false)] public EditorLevelServiceExecutionConfig selfConfig;
        protected override void Process()
        {
            if (this.last == null)
            {
                var rGraphPath = AssetDatabase.GetAssetPath(this.graph);
                LogGame.LogError($"【{rGraphPath}】中节点【{this.name}】未连接原节点");
                return;
            }
            base.Process();
            var rExecution = new EditorLevelServiceExecutionConfig()
            {
                executionType = (byte)this.mServiceType,
                customConfig = this.CreateConfig(),
            };
            if (this.last != null)
            {
                this.last.AddExectuion(rExecution);
                this.next = this.last;
                LevelServiceBlackboard.Instance.AddExecutionGroup(this.executionGroupID, rExecution, this);
            }
            // this.selfConfig = rExecution;
        }

        public LevelServiceCustomConfig CreateConfigTemp()
        {
            return this.CreateConfig();
        }

        protected virtual LevelServiceCustomConfig CreateConfig()
        {
            return null;
        }

        public void Sync(EditorLevelServiceExecutionConfig newConfig, LevelServiceExecutionNode tempNode)
        {
            if (newConfig.executionType == (int)this.mServiceType && tempNode.serviceType == this.serviceType)
            {
                this.OnSync(newConfig, tempNode);
            }
        }

        protected virtual void OnSync(EditorLevelServiceExecutionConfig newConfig, LevelServiceExecutionNode tempNode)
        {
        }

        protected void SyncTarget(EditorLevelServiceDynamicConfig config, string fieldName = "tagOrMcid")
        {
            if (config == null)
            {
                return;
            }
            var rTargetNode =  this.GetOutputNode(fieldName).ToArray().Get(0) as LevelServiceTargetSelectNode;
            rTargetNode?.Sync(config);
        }
        
        protected void SyncTarget(List<EditorLevelServiceDynamicConfig> config, string fieldName = "tagOrMcid")
        {
            if (config == null)
            {
                return;
            }
            var rTargetNode =  this.GetOutputNode(fieldName).ToArray().Get(0) as LevelServiceDynamicListSelectNode;
            rTargetNode?.Sync(config);
        }
        
        protected void SyncDynamic(EditorLevelServiceDynamicConfig config, string fieldName)
        {
            if (config == null)
            {
                return;
            }
            var rDynamicNode =  this.GetOutputNode(fieldName).ToArray().Get(0) as LevelServiceTargetSelectNode;
            rDynamicNode?.Sync(config);
        }
    }
}